package cn.amaake.magicplugin.satoken.filter;

import cn.amaake.magicplugin.satoken.config.MagicApiSaTokenConfig;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

public class TokenResponseHeaderFilter extends OncePerRequestFilter {

    private MagicApiSaTokenConfig magicApiSaTokenConfig;

    public TokenResponseHeaderFilter(MagicApiSaTokenConfig magicApiSaTokenConfig){
        this.magicApiSaTokenConfig = magicApiSaTokenConfig;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.addHeader("token", httpServletRequest.getHeader(magicApiSaTokenConfig.getSaToken().getTokenName()));
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }


}